package com.sunmouse.toollib.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.support.annotation.DrawableRes;
import android.support.annotation.StyleRes;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.sunmouse.toollib.R;

/**
 * Created by Kvin on 2017/6/8.
 */
public class TipsUtils {
    private TextView mTitleTv;
    private TextView mDescTv;
    private Button cancelBtn;
    private Button confirmBtn;

    private static AlertDialog mDialog;


    private TipsUtils(View v, Context context) {
        this.mTitleTv = (TextView) v.findViewById(R.id.title_tv);
        this.mDescTv = (TextView) v.findViewById(R.id.desc_tv);
        this.cancelBtn = (Button) v.findViewById(R.id.cancel_btn);
        this.confirmBtn = (Button) v.findViewById(R.id.confirm_btn);

        //build dialog
        AlertDialog.Builder builder = new AlertDialog.Builder(context, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
        builder.setView(v);
        mDialog = builder.create();
        mDialog.getWindow().setWindowAnimations(R.style.Transition_TopInTopOut);

        mDialog.onAttachedToWindow();
    }

    /**
     * get an instance
     */
    public static TipsUtils getInstance(Context context) {
        View v = LayoutInflater.from(context).inflate(R.layout.dialog_tips, null);
        return new TipsUtils(v, context);
    }

    /**
     * set layout params
     *
     * @param w
     * @param h
     */
    public void setLayoutParams(int w, int h) {
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.width = w;
        lp.height = h;
        setLayoutParams(lp);
    }

    public void setLayoutParams(WindowManager.LayoutParams lp) {
        if (mDialog == null) return;
        mDialog.getWindow().setAttributes(lp);
    }

    /**
     * set outside click event
     */
    public void setCanceledOnTouchOutside(boolean tag) {
        if (mDialog == null) return;
        mDialog.setCanceledOnTouchOutside(tag);
    }

    /**
     * set button background
     */
    public void setCancelBackground(@DrawableRes int res) {
        if (res < 0) {
            throw new RuntimeException("res is invalid");
        }
        this.cancelBtn.setBackgroundResource(res);
    }

    public void setConfirmBackground(@DrawableRes int res) {
        if (res < 0) {
            throw new RuntimeException("res is invalid");
        }
        this.confirmBtn.setBackgroundResource(res);
    }


    /**
     * show or hide
     */
    public void show() {
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
    }

    public void dismiss() {
        if (mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    /**
     * set text
     */
    public void setTitle(String title) {
        this.mTitleTv.setText(TextUtils.isEmpty(title) ? "" : title);
    }

    public void setDesc(String title) {
        this.mDescTv.setText(TextUtils.isEmpty(title) ? "" : title);
    }

    public void setCancelTxt(String txt) {
        this.cancelBtn.setText(txt);
    }

    public void setConfirmTxt(String txt) {
        this.confirmBtn.setText(txt);
    }

    /**
     * set transition animation
     */
    public void setAnim(@StyleRes int anim) {
        if (mDialog == null) return;
        mDialog.getWindow().setWindowAnimations(anim);
    }

    /**
     * get id of cancel view
     */
    public int getCancelId() {
        if (cancelBtn == null) return -1;
        return cancelBtn.getId();
    }

    /**
     * get id of confirm view
     */
    public int getConfirmId() {
        if (confirmBtn == null) return -1;
        return confirmBtn.getId();
    }


    /**
     * set click event
     */
    public void setOnCancelClickListener(View.OnClickListener listener) {
        if (listener == null) {
            this.cancelBtn.setOnClickListener(mListener);//default handle method
            return;
        }
        this.cancelBtn.setOnClickListener(listener);
    }

    public void setOnConfirmClickListener(View.OnClickListener listener) {
        if (listener == null) {
            this.confirmBtn.setOnClickListener(mListener);
            return;
        }
        this.confirmBtn.setOnClickListener(listener);
    }


    /**
     * default click event
     */
    private View.OnClickListener mListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismiss();
        }
    };

}
